home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7751 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  82 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!eskimo!scs
  3. From: scs@eskimo.com (Steve Summit)
  4. Subject: why arrays may seem like pointers (was: What is &Variable ... )
  5. X-Nntp-Posting-Host: eskimo.com
  6. Message-ID: <DnHyrp.CF8@eskimo.com>
  7. Sender: news@eskimo.com (News User Id)
  8. Organization: schmorganization
  9. References: <4gsdno$1bg@umbc9.umbc.edu> <4gtab6$acb@ceylon.gte.com> <313318b8.53776146@nntp.ix.netcom.com>
  10. Date: Wed, 28 Feb 1996 17:47:49 GMT
  11.  
  12. In article <313318b8.53776146@nntp.ix.netcom.com>, miker3@ix.netcom.com
  13. (Mike Rubenstein) writes:
  14. > Brenda <g051286> wrote:
  15. >> schlein@umbc.edu (Jonas J. Schlein) wrote:
  16. >>> Yes it could and yes it is...'myarray' is not a pointer, but &myarray is
  17. >>> a pointer to 'myarray'.
  18. >> 
  19. >> Um, that's not correct.  myarray is DEFINITELY a pointer! ...
  20. >
  21. > NO. NO. NO.  Where do people get this idea that arrays are pointers.
  22. > Arrays are arrays and pointers are pointers.  In many, but not all,
  23. > situations an array is converted to a pointer.
  24.  
  25. I know of at least two reasons why confusion here persists.
  26. The first, of course, is that as formal parameters to functions,
  27. what look like (that is, what may be declared as) arrays are
  28. really pointers.  It's assumed that some programmers read more
  29. into this facet of C's pointer/array "equivalence" than is the
  30. case.  (It's been suggested that the pagination between pages 99
  31. and 100 of K&R2 doesn't help any, although I sincerely hope that
  32. no one tries to learn C by opening K&R2 at random and reading the
  33. top of page 100 in isolation.)
  34.  
  35. I recently discovered a second reason.  I teach a small, informal
  36. class on C programming, and one of my main reasons for teaching
  37. it is so that *I* can learn where some of these perpetual
  38. misconceptions and difficulties come from.  I've noticed that if
  39. I say, "In blah blah blah, an array is converted into a pointer,"
  40. some students hear that and think that the array itself has been
  41. converted into a pointer, now and forever.  (Perhaps this notion
  42. is reinforced by the fact that, in a few other situations, such
  43. as function parameters, C does rewrite declarations for you.)
  44. In other words, when you say "When an array reference appears in
  45. an expression, it is converted into a pointer," what a student
  46. may hear and learn is something like "When you reference an array
  47. in an expression, the array is changed into a pointer."  It's a
  48. small step from there to noticing that, since you can hardly use
  49. an array without referencing it in some expression somewhere in
  50. your program, by the time you're done, all your arrays will have
  51. been changed into pointers, and you won't have any arrays left.
  52.  
  53. (In a similar vein, I've had a student ask whether code like
  54.  
  55.     int i = 1, j = 2;
  56.     float f;
  57.     f = (float)i / j;
  58.  
  59. changed i into a float permanently.  Would this make any sense at
  60. all?  Not to those of us who know C already, but a student, by
  61. definition, doesn't, and can form the most marvelously peculiar
  62. models is left to his own devices.)
  63.  
  64. Having learned this, I'm now careful to say things like, "When
  65. you make reference to an array in an expression, the compiler
  66. automatically generates a pointer to its first element, just as
  67. if you'd written &array[0]."  I try to use words like "generate,"
  68. and *not* to use words like "convert."
  69.  
  70. (As a final note: I composed this article to answer Mike's
  71. question, which is a good one, and not to poke any fun at him,
  72. but you might be intrigued if you go back, as I just did, and
  73. look at the words he happened to use.)
  74.  
  75.                     Steve Summit
  76.                     scs@eskimo.com
  77. -- 
  78. The Communications Decency Act within the Telecommunications Act
  79. of 1996 (U.S.) is an annoying, threatening, abusive, indecent,
  80. and obscene piece of legislation which attempts to ban annoying,
  81. threatening, abusive, indecent, or obscene communication.
  82.